package com.saibo.spider.gather;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.saibo.spider.commons.SubmitRedisCommons;
import com.saibo.spider.pojo.Douban_film_comment;
import com.saibo.spider.utils.DateUtil;
import com.saibo.spider.utils.EncodeUtil;
import com.saibo.spider.utils.FileUtil;
import com.saibo.spider.utils.HttpRequestUtil;
import com.saibo.spider.utils.HttpResponseUtil;
import com.saibo.spider.utils.MD5Util;
import com.saibo.spider.utils.StringUtil;

/**
 * 一星:很差 1 二星:较差 2 三星:还行 3 四星:推荐 4 五星:力荐 5
 * 
 * 
 **/
public class DoubanFilm {

	
	// Map<String,String> pj = new HashMap<String,String>();
	String url;
	String film_rowkey ;

	public DoubanFilm(String film_rowkey,String url) {
		this.url = url;
		this.film_rowkey = film_rowkey;
	}

	public static void main(String[] args) throws InterruptedException {
		DoubanFilm df = new DoubanFilm("孤芳不自赏","https://movie.douban.com/subject/26608228/comments?status=P");
		df.connect();
	}

	public void connect() throws InterruptedException {
		for (int i = 0; i < 10000; i++) {
			CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, requestHeader(url));
			String html = HttpResponseUtil.getHtml(httpResponse);
			if(null==html){
				for (int j = 0; j < 5; j++) {
					httpResponse = HttpRequestUtil.getRequestMethod(url, requestHeader(url));
					html = HttpResponseUtil.getHtml(httpResponse);
					if(null!=html){
						break;
					}
				}
			}
			String nextPage = parse(html);
			url = nextPage;
			System.out.println(nextPage);
			if (null == nextPage) {
				break;
			}
			Thread.sleep(5 * 1000);
		}
		System.out.println("采集结束");
	}

	public String parse(String html) {
		List<Douban_film_comment> lmss = new ArrayList<Douban_film_comment>();
		Document document = Jsoup.parse(html);
		// FileUtil.writeLogToTxt(document.toString(), "D:/doubanFile.txt");
		Elements elements = document.select("#content>div>div.article>div.mod-bd>div.comment-item");
		for (Element element : elements) {
			Douban_film_comment dfc = new Douban_film_comment();
			// System.out.println(element);
			Element nickname = element.select("div>div.avatar>a").first();
			// System.out.print(""+nickname.attr("title")+"\t");
			dfc.setComment_people_nickname(nickname.attr("title"));
			dfc.setComment_people_url(nickname.attr("href"));
			dfc.setComment_people_rowkey(MD5Util.GET16BITMD5(nickname.attr("href")));
			dfc.setFilm_rowkey(film_rowkey);
			Element votes = element.select("div>div.comment>h3>span>span.votes").first();
			// System.out.print(""+StringUtil.element2text(votes)+"\t");
			dfc.setLike_num(Integer.parseInt(StringUtil.element2text(votes)));
			Element allstar = element.select("div>div.comment>h3>span.comment-info>span.rating").first();
			if (null == allstar) {
				dfc.setScore(0);
			} else {
				if("很差".equals(allstar.attr("title").replaceAll(" ", ""))){
					dfc.setScore(1);
				}else if("较差".equals(allstar.attr("title").replaceAll(" ", ""))){
					dfc.setScore(2);
				}else if("还行".equals(allstar.attr("title").replaceAll(" ", ""))){
					dfc.setScore(3);
				}else if("推荐".equals(allstar.attr("title").replaceAll(" ", ""))){
					dfc.setScore(4);
				}else if("力荐".equals(allstar.attr("title").replaceAll(" ", ""))){
					dfc.setScore(5);
				}else{
					dfc.setScore(0);
				}
			}
			Element comment_time = element.select("div>div.comment>h3>span.comment-info>span.comment-time").first();
			//System.out.println("" + StringUtil.element2text(comment_time) + "\t");
			String time = StringUtil.element2text(comment_time);
			Date dd = DateUtil.stringToDate(time);
			dfc.setComment_date(dd);
			Element comment_msg = element.select("div>div.comment>p").first();
			dfc.setComment_msg(StringUtil.element2text(comment_msg));
			// System.out.println(""+StringUtil.element2text(comment_msg));
			lmss.add(dfc);
		}

		System.out.println("------------------------------------准备提交数据，长度:"+lmss.size());
		SubmitRedisCommons.submitRedisData("douban_file_comment_datalist", StringUtil.objectToJson(lmss));
		Element nextPage = document.select("#paginator>a.next").first();
		if (null == nextPage) {
			System.out.println("没有请求到下一页地址");
		} else {
			String url = "https://movie.douban.com/subject/26608228/comments"
					+ EncodeUtil.decodeURL(nextPage.attr("href"), "UTF-8");
			return url;
		}
		return null;
	}

	public Map<String, String> requestHeader(String url) {
		Map<String, String> mss = new HashMap<String, String>();
		mss.put("Cookie", FileUtil.txt2String(new File("D:/douban.txt")));
		mss.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		mss.put("Accept-Encoding", "gzip, deflate, sdch, br");
		mss.put("Connection", "keep-alive");
		mss.put("Host", "movie.douban.com");
		mss.put("Referer", url);
		mss.put("Upgrade-Insecure-Requests", "1");
		mss.put("User-Agent",
				"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");
		return mss;
	}
}
